<template>
  <div>
    <!-- //面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="12">
          <el-input placeholder="请输入内容" clearable>
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单表格区域 -->
      <el-table stripe border :data="orderInfo" style="width: 100%">
        <el-table-column type="index" label="#" width="50"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="80"></el-table-column>
        <el-table-column prop="pay_status" label="是否付款" width="100">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.pay_status == '0' ? 'warning':'primary'"
            >{{scope.row.pay_status == "0" ? '未付款':'已付款'}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="80"></el-table-column>
        <el-table-column prop="create_time" label="下单时间" width="180">
          <template slot-scope="scope">{{scope.row.create_time | Ctime}}</template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="openAddress(scope.rpw)"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-location"
              size="mini"
              @click="openwuliu(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页列表 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 物流信息对话框 -->
    <el-dialog title="物流信息" :visible.sync="wuliuVisible" width="50%" :before-close="wuliuClose">
      <el-timeline>
        <el-timeline-item v-for="(item,i) in activities" :key="i">
          {{item.time}}
          <br />
          {{item.context}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
    <!-- 编辑收货地址对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addressDialogVisible"
      width="50%"
      :before-close="closeAddress"
    >
      <el-form
        :model="addressForm"
        :rules="rules"
        ref="addressForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="省/市/区">
          <el-cascader
            :props="city"
            clearable
            v-model="selc_cateCity"
            :options="allCity"
            expand-trigger="hover"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="addressForm.address"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeAddress">取 消</el-button>
        <el-button type="primary" @click="AddressTrue">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import City from './citydata.js'
export default {
  data() {
    return {
      addressDialogVisible: false,
      wuliuVisible: false,
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      orderInfo: [],
      total: 0,
      activities: [],
      addressForm: {
        address: ''
      },
      rules: {
        address: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            min: 1,
            max: 100,
            message: '长度在 1 到 100 个字符',
            trigger: 'blur'
          }
        ]
      },
      selc_cateCity: [],
      allCity: City,
      city: {
        label: 'label',
        value: 'value',
        children: 'children'
      }
    }
  },
  created() {
    this.getOderList()
  },
  methods: {
    // 获取订单信息函数
    async getOderList() {
      const { data: res } = await this.$http.get('/orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200)
        return this.$message.error('获取订单失败~请稍后再试~')
      console.log(res)

      this.orderInfo = res.data.goods
      this.total = res.data.total
    },
    // 每页条数发生改变的触发的函数
    handleSizeChange(size) {
      this.queryInfo.pagesize = size
      this.getOderList()
    },
    // 当前页发生改变时发生的函数
    handleCurrentChange(num) {
      this.queryInfo.pagenum = num
      this.getOderList()
    },
    // 物流查询页面关闭函数
    wuliuClose() {
      this.wuliuVisible = false
    },
    // 点击显示物流信息对话框
    async openwuliu(order) {
      console.log(order.order_number)

      this.wuliuVisible = true
      const { data: res } = await this.$http.get(
        '/kuaidi/' + order.order_number
      )
      if (res.meta.status !== 200)
        return this.$message.error('查询物流信息失败~')
      this.activities = res.data
    },
    closeAddress() {
      this.addressDialogVisible = false
    },
    AddressTrue() {
      if (this.selc_cateCity[0]) {
        this.$refs.addressForm.validate(valid => {
          if (!valid) return this.$message.error('请输入正确的信息哦~')
          this.selc_cateCity = []
          this.addressForm.address = ''
          this.addressDialogVisible = false
        })
      } else {
        this.$message.error('还没有选择城市呢~')
      }
    },
    openAddress(order) {
      this.addressDialogVisible = true
    }
  },
  filters: {
    // 时间格式化
    Ctime(val) {
      let time = new Date(val)
      let y = time.getFullYear()
      let M = (time.getMonth() + 1 + '').padStart(2, '0')
      let D = (time.getDate() + '').padStart(2, '0')
      let h = (time.getHours() + '').padStart(2, '0')
      let m = (time.getMinutes() + '').padStart(2, '0')
      let s = (time.getSeconds() + '').padStart(2, '0')
      return `${y}-${M}-${D} ${h}:${m}:${s}`
    }
  }
}
</script>

<style lang="less" scoped>
@import url('../../plugins/timeline/timeline.css');
@import url('../../plugins/timeline-item/timeline-item.css');

.el-table {
  margin-top: 15px;
  margin-bottom: 20px;
}
.el-cascader {
  width: 100%;
}
</style>